package com.dragon.logical.service.avttendance.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.entity.Announcement;
import com.dragon.repository.mapper.AnnouncementMapper;
import com.dragon.logical.service.avttendance.IAnnouncementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-26
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements IAnnouncementService {

    @Autowired
    AnnouncementMapper annMapper;

    @Override
    public Page<Announcement> announcementList(Integer page, Integer limit) {
        Page<Announcement> announcementPage = new Page<>(page, limit);
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Announcement::getStatus,0)
                .orderByDesc(Announcement::getCreateTime);
        return annMapper.selectPage(announcementPage,wrapper);
    }

    @Override
    public void add(Announcement announcement) {
        announcement.setCreateTime(DateUtil.date());
        announcement.setStatus(0);
        annMapper.insert(announcement);
    }

    @Override
    public void delete(Integer id){
        annMapper.deleteById(id);
    }

    @Override
    public void DisableAnnounce(Integer id){
        annMapper.changeStatus(id);
    }

    @Override
    public Announcement getLatestOne(){
        Announcement latestOne = annMapper.getLatestOne();
        if (latestOne == null){
            return new Announcement();
        }
        return annMapper.getLatestOne();
    }
}
